from rest_framework.test import APITestCase
from rest_framework import status
from apiapp.models import User, Tour ,UserTour

class APITestCase(APITestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='testuser', email='test@example.com', password='password123')

    #Тестирование авторизации
    def test_api_v1_login_with_valid_credentials(self):
        data = {
            'email': 'test@example.com',
            'password': 'password123',
        }
        response = self.client.post('/api/v1/login/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    #Тестирование авторизации с невалидными полями
    def test_api_v1_login_with_invalid_credentials(self):
        data = {
            'email': 'test@example.com',
            'password': 'incorrectpassword',
        }
        response = self.client.post('/api/v1/login/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    #Тестирование регистрации
    def test_api_v1_register_with_valid_credentials(self):
        data = {
            'first_name': 'Test',
            'last_name': 'User',
            'email': 'testreg@example.com',
            'password': 'password123',
        }
        response = self.client.post('/api/v1/register/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    #Тестирование регистрации с невалиидными полями
    def test_api_v1_register_with_invalid_credentials(self):
        data = {
            'first_name': 'Test',
            'last_name': 'User',
            'email': 'test@example.com',
            'password': '',  # password is empty
        }
        response = self.client.post('/api/v1/register/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    #Тестирование получения списка тура
    def test_api_v1_tour_list(self):
        response = self.client.get('/api/v1/tour/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    #Тестирование создания тура
    def test_api_v1_tour_create(self):
        data = {
            'name': 'Test Tour',
            'description': 'Test Description',
            'price': 100,
            'seats': 10,
            'arrivalDate': '2022-01-01',
            'departureDate': '2022-01-02',
        }
        response = self.client.post('/api/v1/tour/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

